﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace OnlineManagement.Web.webs
{
    public partial class SowOperations : BasePage
    {
        protected string _sowId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string preUrl = string.Empty;
                if (HttpContext.Current.Request.UrlReferrer != null)
                {
                    preUrl=HttpContext.Current.Request.UrlReferrer.ToString();
                }

                hdPreUrl.Value = preUrl;
                string sowIdStr = Request.QueryString["id"]==null?string.Empty: Request.QueryString["id"].ToString();
                _sowId = sowIdStr;
                //繁殖状态的order id, 1-后备期
                string orderStr = Request.QueryString["order"]==null?string.Empty:Request.QueryString["order"].ToString();
                //繁殖状态下每一个操作的 order id, 1,'发情登记'
                string actionOrderStr = Request.QueryString["actionOrder"]==null?string.Empty: Request.QueryString["actionOrder"].ToString();
                Guid sowId;
                int order;
                int actionOrder;
                if (!Guid.TryParse(sowIdStr, out sowId) 
                    || !int.TryParse(orderStr, out order)
                    ||!int.TryParse(actionOrderStr,out actionOrder))
                {
                    panError.Visible = true;
                    PanSowOpeation.Visible = false;
                    return;
                }
                PanSowOpeation.Visible = true;
                panError.Visible = false;
                LoadUserControl(sowId, order, actionOrder);
            }
        }
        /// <summary>
        /// 母猪繁殖期顺序，order-1-后备期(actionOrder-1,'发情登记',2,'转栏登记',3,'预防保健登记',4,'诊疗登记',5,'淘汰销售登记',6,'死亡登记')
        /// </summary>
        /// <param name="sowId"></param>
        /// <param name="order">1-后备期,2-发情配种期，3-怀孕期，4-分娩期，5-哺乳期，6-空怀期</param>
        /// <param name="actionOrder">后备期(actionOrder-1,'发情登记',2,'转栏登记',3,'预防保健登记',4,'诊疗登记',5,'淘汰销售登记',6,'死亡登记')</param>
        private void LoadUserControl(Guid sowId, int order, int actionOrder) 
        {
            try
            {
            BLL.Sow bllSow = new BLL.Sow();
            DataSet ds = bllSow.GetSowActions();
            EnumerableRowCollection<DataRow> query = null;
            string controlPath = string.Empty;
            if (ds != null && ds.Tables.Count > 0)
            {
                query = from data in ds.Tables[0].AsEnumerable()
                        where data.Field<Byte>("PropagateStateOrder") == order && data.Field<Byte>("ActionOrder") == actionOrder
                        select data;
                if(query!=null&&query.Count()>0){
                  controlPath = query.ToList()[0][4].ToString();
                }
            }
           
                Control control = null;
                if (!string.IsNullOrEmpty(controlPath))
                {
                    control = Page.LoadControl(controlPath);
                }
                if (control != null)
                {
                    this.PanSowOpeation.Controls.Add(control);
                }
                else {
                    this.PanSowOpeation.Visible = false;
                    this.panError.Visible = true;

                }
            }catch(Exception ex){
                lbMsg.Text = "抱歉，服务器发生错误了，我们会尽快修复的.";
            }
        }

    }
}